package com.sam.im.heiqun.uis.dialogs;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.adapters.holder.PopListAdapter;

import java.util.List;

/**
 * Created by wangguodong on 2017/1/10.
 */
public class PopupWindowManages {

    private static Context mContext ;
    private static OnPopSelectId mOnPopSelectId ;
    private static OnPopSelectIdStr mOnPopSelectIdStr;

    public interface OnPopSelectId
    {
        void onClick(int whichButton);
    }

    public interface OnPopSelectIdStr
    {
        void onOkClick(int whichButton, String content);
    }

    public static void initListSelect(final Context context, View view, List<String> mStrs, final OnPopSelectIdStr onPopSelectIdStr){
        mContext = context ;
        mOnPopSelectIdStr = onPopSelectIdStr ;
        ShowListSelect(view,mStrs);
    }
    private static void ShowListSelect(View view, final List<String> mStrs){
        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(mContext).inflate(
                R.layout.pop_window_list, null);
        ListView list_poop = (ListView) contentView.findViewById(R.id.list_poop);
        PopListAdapter mPopListAdapter = new PopListAdapter(mContext,mStrs);
        list_poop.setAdapter(mPopListAdapter);


        final PopupWindow popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("mengdd", "onTouch : ");
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(
                R.drawable.xmxq_tc_bj));
        list_poop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                popupWindow.dismiss();
                if (null!=mOnPopSelectIdStr){
                    mOnPopSelectIdStr.onOkClick(i,mStrs.get(i));
                }
            }
        });
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);

    }

}
